package com.sanpu.kxe.business.main.model;

import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.sanpu.kxe.business.main.bean.ActHomePopOutput;
import com.sanpu.kxe.business.main.bean.AdviceOutput;
import com.sanpu.kxe.business.main.bean.ApplyCardOutput;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.bean.CheckVersionOutput;
import com.sanpu.kxe.business.main.bean.CreditEventOutput;
import com.sanpu.kxe.business.main.bean.EventCenterOutput;
import com.sanpu.kxe.business.main.bean.HomeBannerOutput;
import com.sanpu.kxe.business.main.bean.HomeCenterOutput;
import com.sanpu.kxe.business.main.bean.HomePageOutput;
import com.sanpu.kxe.business.main.bean.IntegralConvertOutput;
import com.sanpu.kxe.business.main.bean.InviteFriendsOutput;
import com.sanpu.kxe.business.main.bean.InviteMakeMoneyOutput;
import com.sanpu.kxe.business.main.bean.LinkfaceCheckOutput;
import com.sanpu.kxe.business.main.bean.MaterialPosterOutput;
import com.sanpu.kxe.business.main.bean.MaterialVideoOutput;
import com.sanpu.kxe.business.main.bean.MyProfitsOutput;
import com.sanpu.kxe.business.main.bean.NewsColumnsOutput;
import com.sanpu.kxe.business.main.bean.NewsContentOutput;
import com.sanpu.kxe.business.main.bean.NoticeOutput;
import com.sanpu.kxe.business.main.bean.PartnerAllyOutput;
import com.sanpu.kxe.business.main.bean.PartnerInfoOutput;
import com.sanpu.kxe.business.main.bean.PointDetailOutput;
import com.sanpu.kxe.business.main.bean.PointTotalOutput;
import com.sanpu.kxe.business.main.bean.PromotePosterOutput;
import com.sanpu.kxe.business.main.bean.PushDetailsOutput;
import com.sanpu.kxe.business.main.bean.PushMaterialOutput;
import com.sanpu.kxe.business.main.bean.RebateTotalOutput;
import com.sanpu.kxe.business.main.bean.ReferrerOutput;
import com.sanpu.kxe.business.main.bean.ShareNumOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.bean.UserSignOutput;
import com.sanpu.kxe.business.main.bean.WithdrawSignOutput;
import com.sanpu.kxe.business.main.bean.WxInfoOutput;
import com.sanpu.kxe.rxservice.DataUtils;
import com.sanpu.kxe.rxservice.client.MainClient;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import rx.Observable;

/**
 * Description：Main模块model
 * Created by 薛勇军 on 2018/7/9.
 */

public class MainModel {

    private static volatile MainModel mInstance;

    private MainModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static MainModel getInstance() {
        MainModel result = mInstance;
        if (result == null) {
            synchronized (MainModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new MainModel();
                }
            }
        }
        return result;
    }

    /**
     * 获取个人信息
     */
    public Observable<UserCenterOutput> userCenter() {
        return MainClient.getInstance().userCenter();
    }

    /**
     * 获取个人配置信息
     */
    public Observable<UserConfigOutput> userConfig() {
        return MainClient.getInstance().userConfig();
    }

    /**
     * 首页广告
     */
    public Observable<List<ActHomePopOutput>> actAppadlist() {
        return MainClient.getInstance().actAppadlist();
    }

    /**
     * 获取推荐人的信息
     */
    public Observable<ReferrerOutput> getReferrerUserInfo() {
        return MainClient.getInstance().getReferrerUserInfo();
    }

    /**
     * 修改昵称
     */
    public Observable<String> updateNickName(String nickName) {
        JSONObject reqData = new JSONObject();
        try {
            reqData.put("nickName", URLEncoder.encode(nickName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return MainClient.getInstance().updateNickName(reqData);
    }

    /**
     * 获取我的微信信息
     */
    public Observable<WxInfoOutput> getWxInfo() {
        return MainClient.getInstance().getWxInfo();
    }

    /**
     * 上传我的微信信息
     */
    public Observable<String> postWxQrcode(String wxChatId, HashMap<String, File> files) {
        JSONObject reqData = new JSONObject();
        reqData.put("wxChatId", wxChatId);
        if (files != null && !files.isEmpty()) {
            return MainClient.getInstance().postWxQrcode(reqData, files);
        } else {
            return MainClient.getInstance().postWxQrcode(reqData);
        }
    }

    /**
     * 获取活动中心
     */
    public Observable<List<EventCenterOutput>> getActInfList() {
        return MainClient.getInstance().getActInfList();
    }

    /**
     * 合伙人信息
     */
    public Observable<PartnerInfoOutput> partnerInf() {
        return MainClient.getInstance().partnerInf();
    }

    /**
     * 合伙人盟友列表
     */
    public Observable<List<PartnerAllyOutput>> partnerAllyList(int nowPage) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        return MainClient.getInstance().partnerAllyList(reqData);
    }

    /**
     * 获取认证信息
     */
    public Observable<CertificationOldOutput> getAuthInfo() {
        return MainClient.getInstance().getAuthInfo();
    }

    /**
     * 四要素校验
     */
    public Observable<String> authentication(String name, String identityNumber,
                                             String bankCardNumber, String bankPhoneNumber) {
        JSONObject reqData = new JSONObject();
        try {
            name = URLEncoder.encode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        reqData.put("uName", name);
        reqData.put("uCid", identityNumber);
        reqData.put("uBankId", bankCardNumber);
        reqData.put("uPhone", bankPhoneNumber);
        return MainClient.getInstance().authentication(reqData);
    }

    /**
     * 提交实名认证信息
     */
    public Observable<String> realNamePicAuth(CertificationOldOutput mOutput, HashMap<String, File> imgFiles) {
        JSONObject reqData = new JSONObject();
        if (mOutput != null) {
            String name;
            try {
                name = URLEncoder.encode(mOutput.getCrpNm(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                name = mOutput.getCrpNm();
            }
            reqData.put("uName", name);
            reqData.put("uCid", mOutput.getCrpIdNo());
            reqData.put("uBankId", mOutput.getBankNo());
            reqData.put("uPhone", mOutput.getRsdTel());
        }
        return MainClient.getInstance().realNamePicAuth(reqData, imgFiles);
    }

    /**
     * 上传身份证正反面
     */
    public Observable<CertificationOutput> uploadIdCardInfo(@NonNull HashMap<String, File> imgFiles) {
        return MainClient.getInstance().uploadIdCardInfo(imgFiles);
    }

    /**
     * 上传银行卡正反面及手持
     */
    public Observable<CertificationOutput> uploadBankInfoAndFaceMatch(String idFroPicPath, String idCardNo,
                                                                      @NonNull HashMap<String, File> imgFiles) {
        JSONObject reqData = new JSONObject();
        reqData.put("idFroPicPath", idFroPicPath);
        reqData.put("idCardNo", idCardNo);
        return MainClient.getInstance().uploadBankInfoAndFaceMatch(reqData, imgFiles);
    }

    /**
     * 上传银行卡正反面及手持
     */
    public Observable<String> realnameAuthentication(@NonNull CertificationOutput certificationOutput) {
        String idCardName = null, idCardAddress = null, bankName = null;
        try {
            idCardName = URLEncoder.encode(certificationOutput.getIdCardName(), "UTF-8");
            idCardAddress = URLEncoder.encode(certificationOutput.getIdCardAddress(), "UTF-8");
            bankName = URLEncoder.encode(certificationOutput.getBankName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject reqData = JSONObject.parseObject(JSONObject.toJSON(certificationOutput).toString());
        reqData.put("idCardName", idCardName);
        reqData.put("idCardAddress", idCardAddress);
        reqData.put("bankName", bankName);
        return MainClient.getInstance().realnameAuthentication(reqData);
    }

    /**
     * 上传银行卡正反面及手持
     */
    public Observable<String> liveness(@NonNull CertificationOutput certificationOutput, File protobufFile) {
        String idCardName = null, idCardAddress = null, bankName = null;
        try {
            idCardName = URLEncoder.encode(certificationOutput.getIdCardName(), "UTF-8");
            idCardAddress = URLEncoder.encode(certificationOutput.getIdCardAddress(), "UTF-8");
            bankName = URLEncoder.encode(certificationOutput.getBankName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject reqData = JSONObject.parseObject(JSONObject.toJSON(certificationOutput).toString());
        reqData.put("idCardName", idCardName);
        reqData.put("idCardAddress", idCardAddress);
        reqData.put("bankName", bankName);

        HashMap<String, File> hashMap = new HashMap<>();
        hashMap.put("livenessDataFile", protobufFile);
        return MainClient.getInstance().liveness(reqData, hashMap);
    }

    /**
     * linkface鉴权
     */
    public Observable<LinkfaceCheckOutput> auth(String idCardName, String idCardNo, String cardNo, String phoneNo) {
        JSONObject reqData = new JSONObject();
        try {
            idCardName = URLEncoder.encode(idCardName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        reqData.put("idCardName", idCardName);
        reqData.put("idCardNo", idCardNo);
        reqData.put("cardNo", cardNo);
        reqData.put("phoneNo", phoneNo);
        return MainClient.getInstance().auth(reqData);
    }

    /**
     * 积分统计
     */
    public Observable<PointTotalOutput> pointTotal() {
        return MainClient.getInstance().pointTotal();
    }

    /**
     * 积分明细列表
     */
    public Observable<List<PointDetailOutput>> pointDetailList(int nowPage) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        return MainClient.getInstance().pointDetailList(reqData);
    }

    /**
     * 消息列表
     */
    public Observable<List<NoticeOutput>> messageList(int nowPage) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        return MainClient.getInstance().messageList(reqData);
    }

    /**
     * 消息列表
     */
    public Observable<NoticeOutput> messageDetail(String id) {
        JSONObject reqData = new JSONObject();
        reqData.put("id", id);
        return MainClient.getInstance().messageDetail(reqData);
    }

    /**
     * 标记消息已读
     */
    public Observable<String> markMessageRead(String id) {
        JSONObject reqData = new JSONObject();
        reqData.put("id", id);
        return MainClient.getInstance().markMessageRead(reqData);
    }

    /**
     * 分享记录
     * @param shareType WXCIRCLE:微信朋友圈
     *                  WX：     微信好友
     *                  QQ：     QQ好友
     *                  QZONE：  QQ空间
     */
    public Observable<String> sharingIntegral(String shareType) {
        JSONObject reqData = new JSONObject();
        reqData.put("shareType", shareType);
        return MainClient.getInstance().sharingIntegral(reqData);
    }

    /**
     * 首页banner
     */
    public Observable<List<HomeBannerOutput>> banner() {
        return MainClient.getInstance().banner();
    }

    /**
     * 首页banner
     */
    public Observable<List<HomeBannerOutput>> newBanner() {
        return MainClient.getInstance().newBanner();
    }

    /**
     * 首页顶部功能栏
     */
    public Observable<List<HomePageOutput>> homePage() {
        return MainClient.getInstance().homePage();
    }

    /**
     * 首页中间功能栏
     */
    public Observable<List<HomeCenterOutput>> findHomeCenter() {
        return MainClient.getInstance().findHomeCenter();
    }

    /**
     * 办理信用卡
     */
    public Observable<List<ApplyCardOutput>> applyCard() {
        return MainClient.getInstance().applyCard();
    }

    /**
     * 今日签到
     */
    public Observable<UserSignOutput> userSign() {
        return MainClient.getInstance().userSign();
    }

    /**
     * 卡积分兑换
     */
    public Observable<IntegralConvertOutput> integralConvert() {
        return MainClient.getInstance().integralConvert();
    }

    /**
     * 佣金统计
     */
    public Observable<RebateTotalOutput> rebateTotal() {
        return MainClient.getInstance().rebateTotal();
    }

    /**
     * 提现签约
     */
    public Observable<WithdrawSignOutput> withdrawSigning() {
        return MainClient.getInstance().withdrawSigning();
    }

    /**
     * 我的团队列表（收益）
     */
    public Observable<MyProfitsOutput> myTeamList(int nowPage, String merchantCode, int curLvl,
                                                  String mAccountName) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        reqData.put("merchantCode", merchantCode);
        reqData.put("curLvl", curLvl);
        reqData.put("accountName", mAccountName);
        return MainClient.getInstance().myTeamList(reqData);
    }

    /**
     * 推广海报朋友圈
     */
    public Observable<List<PromotePosterOutput>> getPosterList(int nowPage) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        return MainClient.getInstance().getPosterList(reqData);
    }

    /**
     * 增加海报分享次数
     */
    public Observable<ShareNumOutput> addShareNum(String posterId) {
        JSONObject reqData = new JSONObject();
        reqData.put("posterId", posterId);
        return MainClient.getInstance().addShareNum(reqData);
    }

    /**
     * 地推物料列表（推广）
     */
    public Observable<List<PushMaterialOutput>> getMaterialList() {
        return MainClient.getInstance().getMaterialList();
    }

    /**
     * 物料详情列表
     */
    public Observable<List<PushDetailsOutput>> getMaterialDetailsList(int nowPage, String currentType) {
        JSONObject reqData = new JSONObject();
//        reqData.put("nowPage", nowPage);
        reqData.put("type", currentType);
        return MainClient.getInstance().getMaterialDetailsList(reqData);
    }

    /**
     * 获取新闻类型
     */
    public Observable<List<NewsColumnsOutput>> getNewsColumns() {
        return MainClient.getInstance().getNewsColumns();
    }

    /**
     * 获取新闻内容
     */
    public Observable<List<NewsContentOutput>> getNewsList(int nowPage, String columnType) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", String.valueOf(nowPage));
        reqData.put("columnType", columnType);
        return MainClient.getInstance().getNewsList(reqData);
    }

    /**
     * 记录点击新闻内容
     */
    public Observable<String> statisticalClick(String newsId) {
        JSONObject reqData = new JSONObject();
        reqData.put("newsId", newsId);
        return MainClient.getInstance().statisticalClick(reqData);
    }

    /**
     * 提交了难任务信息
     */
    public Observable<String> conmitTask(String taskType, String contactName, String contactNumber,
                                         String taskDescription) {
        JSONObject reqData = new JSONObject();
        reqData.put("taskType", taskType);
        reqData.put("contactNumber", contactNumber);
        try {
            reqData.put("contactName", URLEncoder.encode(contactName, "UTF-8"));
            reqData.put("taskDescription", URLEncoder.encode(taskDescription, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return MainClient.getInstance().conmitTask(reqData);
    }

    /**
     * 提交了难任务信息
     */
    public Observable<List<AdviceOutput>> getTaskList(int nowPage) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        return MainClient.getInstance().getTaskList(reqData);
    }

    /**
     * 设置支付密码
     */
    public Observable<String> setPayPassword(String password, String accountName) {
        JSONObject reqData = new JSONObject();
        reqData.put("password", DataUtils.getMD5Str(password + accountName));
        return MainClient.getInstance().setPayPassword(reqData);
    }

    /**
     * 获取APP版本信息
     */
    public Observable<CheckVersionOutput> getAppVersionInfo() {
        JSONObject reqData = new JSONObject();
        reqData.put("appSystemType", "ANDROID");
        return MainClient.getInstance().getAppVersionInfo(reqData);
    }

    /**
     * 信用卡活动
     */
    public Observable<List<CreditEventOutput>> creditcardActList() {
        return MainClient.getInstance().creditcardActList();
    }

    /**
     * 邀请好友
     */
    public Observable<List<InviteFriendsOutput>> inviteFriends() {
        return MainClient.getInstance().inviteFriends();
    }

    /**
     * 邀请赚钱
     */
    public Observable<List<InviteMakeMoneyOutput>> inviteMakeMoney() {
        return MainClient.getInstance().inviteMakeMoney();
    }

    /**
     * 全部应用
     */
    public Observable<List<HomeCenterOutput>> findHomeCenterMore() {
        return MainClient.getInstance().findHomeCenterMore();
    }

    /**
     * 个人头像
     */
    public Observable<String> uploadUserHeadPortrait(@NonNull HashMap<String, File> imgFiles) {
        return MainClient.getInstance().uploadUserHeadPortrait(imgFiles);
    }

    /**
     * 6.10.10推广视频
     */
    public Observable<List<MaterialVideoOutput>> promotionVideo() {
        return MainClient.getInstance().promotionVideo();
    }

    /**
     * 6.10.9推广海报
     */
    public Observable<List<MaterialPosterOutput>> promotionPoster() {
        return MainClient.getInstance().promotionPoster();
    }
}
